﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
namespace BitPortal.Admin.Media.Gallery.Module
{
    public partial class MediaInGallery : System.Web.UI.UserControl
    {

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            wAddMedia.AutoLoad.Url = "/Admin/Media/Media/Default.aspx?AddToGallery=" + txtGalleryID.Text + "&ReloadId=" + gridMediaInGallery.ClientID;
            wAddMedia.LoadContent();
            wAddMedia.Show();
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");
                int record = 0;
                RowSelectionModel sm = this.gridMediaInGallery.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (BitPortal.Business.GalleryBLL.DeleteMedia(Convert.ToInt32(txtGalleryID.Text), Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Xóa " + record + " media khỏi gallery thành công!");
                    BindGridData();
                    sm.ClearSelections();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void BindGridData()
        {
            int galleryID = txtGalleryID.Text.ToInt32TryParse();
            this.Store1.DataSource = DataBLL.DataProcessing(GalleryBLL.GetByID(galleryID).MediasInGalleries.ToList(), null, null);
            this.Store1.DataBind();
        }

        public void BindData(int galleryID)
        {
            Models.Gallery g = Business.GalleryBLL.GetByID(galleryID);
            if (g != null)
            {
                Window1.Title = "Media trong Gallery ' " + g.Name + " '";
                txtGalleryID.Text = g.GalleryID.ToString();
                this.Store1.DataSource = DataBLL.DataProcessing(g.MediasInGalleries.ToList(), null, null);
                this.Store1.DataBind();
                this.Window1.Show();
            }
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            BindGridData();
        }

        protected void HandleChanges(object sender, BeforeStoreChangedEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("MediaEdit");
                ChangeRecords<MediasInGallery> resources = e.DataHandler.ObjectData<MediasInGallery>();
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                int galleryID = txtGalleryID.Text.ToInt32TryParse();

                foreach (MediasInGallery record in resources.Updated)
                {
                    MediasInGallery obj = bitEnt.MediasInGalleries.SingleOrDefault(ent => ent.MediaID == record.MediaID && ent.GalleryID == galleryID);
                    obj.Title = record.Title;
                    obj.Description = record.Description;
                    obj.SortOrder = record.SortOrder;
                    obj.TargetURL = record.TargetURL;
                }
                if (bitEnt.SaveChanges() > 0)
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Cập nhật thành công!");
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

    }
}